import tkinter as tk
from tkinter import filedialog
import cv2 as cv
from doc_scanner import DocScanner

class DocScannerApp:
    def __init__(self):
        self.doc_scanner = DocScanner()  # Create an instance of DocScanner

        self.corners = None  # Initialize corners where document corners will be stored
        self.dragging_idx = -1  # Variable to track which corner is being dragged (-1 means none)
        self.img = None  # Initialize image variable to hold loaded image

        self.root = tk.Tk()  # Create a Tkinter window

        # Create a canvas in the window to display the image and cropping box
        self.canvas = tk.Canvas(self.root, width=600, height=400)
        self.canvas.pack()  # Add the canvas to the window
        self.canvas.bind("<Button-1>", self.mouse_callback)  # Bind mouse events
        self.canvas.bind("<ButtonRelease-1>", self.mouse_callback)
        self.canvas.bind("<Motion>", self.mouse_callback)

        # Create a "Select Image" button that calls self.select_image when clicked
        btn_select = tk.Button(self.root, text="Select Image", command=self.select_image)
        btn_select.pack(side=tk.LEFT)  # Add button to the left side of the window

        self.show_mouse_move = tk.BooleanVar()  # BooleanVar to store checkbox state

        # Create a checkbox labeled "Show Mouse Move"
        chk_show_mouse_move = tk.Checkbutton(self.root, text="Show Mouse Move", variable=self.show_mouse_move)
        chk_show_mouse_move.pack(side=tk.LEFT)  # Add checkbox to the left side of the window

        # Create a "Crop" button that calls self.crop when clicked
        btn_crop = tk.Button(self.root, text="Crop", command=self.crop)
        btn_crop.pack(side=tk.RIGHT)  # Add button to the right side of the window

    def select_image(self):
        """Select an image file and load it along with document corners."""
        file_path = filedialog.askopenfilename()  # Open file dialog and get selected file path
        self.img, self.corners = self.doc_scanner.load_image(file_path)  # Load image and corners
        img_height, img_width, _ = self.img.shape  # Get image dimensions

        if img_height > 600 or img_width > 800:  # If image dimensions exceed canvas size
            scale = min(600 / img_height, 800 / img_width)  # Calculate scaling factor
            self.img = cv.resize(self.img, (int(img_width * scale), int(img_height * scale)))  # Resize image
            self.corners = self.corners * scale  # Resize corners accordingly
            img_height, img_width, _ = self.img.shape  # Update image dimensions

        self.canvas.config(width=img_width, height=img_height)  # Configure canvas size to match image

    def mouse_callback(self, event):
        # Implement mouse event handling logic here
        pass

    def crop(self):
        # Implement cropping logic here
        pass

    def run(self):
        self.root.mainloop()  # Start the Tkinter main loop

# Main function to run the application
if __name__ == "__main__":
    app = DocScannerApp()
    app.run()
